package hu.bme.mit.rifl.business.nodes;

import hu.bme.mit.rifl.business.base.RiflOrderInputQueue;
import hu.bme.mit.rifl.business.base.RiflOrderOutputQueue;
import hu.bme.mit.rifl.business.base.WFElement;

/**
 * Worker node which serves the order if the repository test returns an
 * "FEASIBLE" state.
 */
public class ServeOrder extends WFElement {

	private static final int RIFL_SERVE2BOOK = 0;
	private static final int RIFL_SUCCESS2FULFILL = 1;

	/**
	 * Default constructor of the node. Sets the name (displayed on the GUI), the
	 * input queue(s) and the output queue(s).
	 */
	public ServeOrder() {
		super("ServeOrder");

		inputs.add(new RiflOrderInputQueue("rifl.check2success"));
		inputs.add(new RiflOrderInputQueue("rifl.book2serve"));
		outputs.add(RIFL_SERVE2BOOK, new RiflOrderOutputQueue("rifl.serve2book"));
		outputs.add(RIFL_SUCCESS2FULFILL, new RiflOrderOutputQueue(
				"rifl.success2fulfill"));
	}

	/**
	 * Business logic.
	 */
	@Override
	public void process() {
		this.order.setState("GETTINGSERVED");
		this.order.setStateCodes(5);
	}

	/**
	 * The output queue is selected with respect to the parallel node of the
	 * workflow, i.e. it's sent to the fulfill queue if it was processed by both
	 * nodes and sent to the another node is it wasn't.
	 */
	@Override
	protected void finish() {
		if (this.order.isBooked()) {
			outputs.get(RIFL_SUCCESS2FULFILL).put(this.order);
			this.order = null;
		} else {
			outputs.get(RIFL_SERVE2BOOK).put(this.order);
			this.order = null;
		}
	}
}
